package logic

import (
	"C/internal/svc"
	"C/models/mysql"
	"C/models/redis"
	"C/shenzhousrv"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *shenzhousrv.UserLoginRequest) (*shenzhousrv.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if redis.Rdb.Get(context.Background(), "sendSmsLogin"+in.Phone).Val() != in.Code {
		return nil, fmt.Errorf("验证码输入错误")
	}
	user.FindUser(in.Phone)
	if user.ID == 0 {
		user.Phone = in.Phone
		user.Status = 1
		err := user.AddUser()
		if err != nil {
			return nil, fmt.Errorf("用户登录失败")
		}
	}

	return &shenzhousrv.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
